package com.yld.car.market;

import java.io.File;
import java.util.HashMap;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.yld.car.common.ConstantUtils;
import com.yld.car.common.NetworkProgressUtils;
import com.yld.car.domain.UserInfo;
import com.yld.car.market.tab.TabActivityGroup;

public class UpdatePasswordActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.setting_password);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
		showBackButton();
		Button localButton = (Button) findViewById(R.id.rightButton);
		localButton.setVisibility(View.VISIBLE);
		localButton.setText("完成");
		localButton.setOnClickListener(completeClickListener);
		((TextView) findViewById(R.id.title)).setText("修改密码");
	}

	// /**
	// * 定义统一的返回按键设置功能
	// */
	// protected void showBackButton() {
	// Button localButton = (Button) findViewById(R.id.leftButton);
	// localButton.setVisibility(View.VISIBLE);
	// localButton.setOnClickListener(new View.OnClickListener() {
	// public void onClick(View paramView) {
	// // finish();
	// Intent intent = new Intent();
	// intent.setClass(UpdatePasswordActivity.this,
	// TabActivityGroup.class);
	// startActivity(intent);
	// }
	// });
	// }

	private InputMethodManager imm;
	private View.OnClickListener completeClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {

			EditText localEditText1 = (EditText) findViewById(R.id.oldPwd);
			final String str1 = localEditText1.getText().toString().trim();
			EditText localEditText2 = (EditText) findViewById(R.id.newPwd);
			final String str2 = localEditText2.getText().toString().trim();
			EditText localEditText3 = (EditText) findViewById(R.id.newPwd2);

			final String str3 = localEditText3.getText().toString().trim();

			imm = ((InputMethodManager) localEditText1.getContext()
					.getSystemService("input_method"));
			if (imm.isActive())
				imm.hideSoftInputFromWindow(
						localEditText1.getApplicationWindowToken(), 0);
			if (imm.isActive())
				imm.hideSoftInputFromWindow(
						localEditText1.getApplicationWindowToken(), 0);
			imm = ((InputMethodManager) localEditText2.getContext()
					.getSystemService("input_method"));
			if (imm.isActive())
				imm.hideSoftInputFromWindow(
						localEditText2.getApplicationWindowToken(), 0);
			imm = ((InputMethodManager) localEditText3.getContext()
					.getSystemService("input_method"));
			if (imm.isActive())
				imm.hideSoftInputFromWindow(
						localEditText3.getApplicationWindowToken(), 0);

			if (str1.equals("")) {
				showDialogFinish("请输入旧密码", false);
				return;
			}
			if (str2.equals("")) {
				showDialogFinish("请输入新密码", false);
				return;
			}
			if (str3.equals("")) {
				showDialogFinish("请输入重复新密码", false);
				return;
			}
			if (str2.length() < 6) {
				showDialogFinish("新密码至少6位", false);
				return;
			}
			if (str3.length() < 6) {
				showDialogFinish("新密码至少6位", false);
				return;
			}

			if (!str2.equals(str3)) {
				showDialogFinish("两次新密码不同", false);
				return;
			}
			localEditText1.clearFocus();
			localEditText2.clearFocus();
			localEditText3.clearFocus();

			AsyncTask<String, String, String> task = new AsyncTask<String, String, String>() {

				@Override
				protected String doInBackground(String... params) {
					//
					// UpdateSelfPassword(string userId, string oldPwd, string
					// newPwd)
					// 参数一：userId:当前用户ID
					// 参数二：oldPwd:旧密码
					// 参数二：newPwd:新密码

					NetworkProgressUtils utils = NetworkProgressUtils
							.getInstance();
					HashMap<String, String> allParams = new HashMap<String, String>();

					UserInfo u = (UserInfo) readObject("userInfo");
					allParams.put("userId", u.getUserphone());
					allParams.put("oldPwd", str1);
					allParams.put("newPwd", str3);
					publishProgress("");
					Object o = utils.getWebservicesByData(
							ConstantUtils.NAME_SPACE,
							ConstantUtils.ALL_METHROD_NAME.get(26),
							ConstantUtils.NAME_SPACE
									+ ConstantUtils.ALL_METHROD_NAME.get(26),
							ConstantUtils.GET_UPDATE_SELF_PASSWORD_URL,
							allParams);

					if (o != null) {
						return o.toString();

					}
					return null;
				}

				@Override
				protected void onProgressUpdate(String... values) {

					showLoading("正在提交......");
					super.onProgressUpdate(values);
				}

				protected void onPostExecute(String result) {

					hideLoading();
					System.out.println("result====" + result);
					if (result != null) {

						if (result.indexOf("修改成功") >= 0) {

							// System.out.println("hahaha===");

							// mApp.exitApp();

							// FileInputStream fis=openFileInput("userInfo");
							File f = UpdatePasswordActivity.this
									.getFileStreamPath("userInfo");
							System.out.println(f.delete());

							Intent intent = new Intent();
							intent.setClass(getApplicationContext(),
									GuidePageActivity.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							showDialogFinish("修改密码成功，请用新密码重新登录", intent);
							//mApp.exitApp();

						}

					}

				}

			};
			task.execute("");

		}

	};

}